package com.itboye.ihomebank.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DayCountUtil {
		static SimpleDateFormat sdf_Date = new SimpleDateFormat("yyyy-MM-dd",
				Locale.getDefault());

		public static String getDay(String str_tiem1, String str_time2) {
			int begin_Year;
			int begin_Day;
			int begin_Moth;
			int end_Year;
			int end_Moth;
			int end_Day;
			Calendar begin_cd = null;
			Calendar end_cd = null;
			try {
				Date begin_dt = sdf_Date.parse(str_tiem1);
				Date end_dt = sdf_Date.parse(str_time2);
				begin_cd = Calendar.getInstance();
				end_cd = Calendar.getInstance();

				begin_cd.setTime(begin_dt);
				end_cd.setTime(end_dt);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			if (begin_cd != null && end_cd != null) {
				begin_Year = begin_cd.get(Calendar.YEAR);
				begin_Moth = begin_cd.get(Calendar.MONTH) + 1;
				begin_Day = begin_cd.get(Calendar.DAY_OF_MONTH);

				end_Year = end_cd.get(Calendar.YEAR);
				end_Moth = end_cd.get(Calendar.MONTH) + 1;
				end_Day = end_cd.get(Calendar.DAY_OF_MONTH);
				System.out.println("-----begin-Year---------*" + begin_Year);
				System.out.println("-----begin-day---------*" + begin_Day);
				System.out.println("-----end-day---------*" + end_Year);
				System.out.println("-----end-day---------*" + end_Day);
				/**
				 * ������С�ڿ�ʼ���������� ������С�ڿ�ʼ���������� ʣ������ �ÿ�ʼ������-��ʼ��+������
				 */
				// ͬ��
				if (begin_Year == end_Year) {
					if (end_Moth - begin_Moth > 1)// ���1����������
					{
						// �����������ڿ�ʼ�������ò���-��֮�¼�һ
						if (end_Day > begin_Day)
							return getData(0, end_Moth - begin_Moth, end_Day
									- begin_Day);
						else
							return getData(0, end_Moth - begin_Moth - 1,
									getMothDay(begin_Year, begin_Moth)
											- begin_Day + end_Day);
					} else if (end_Moth - begin_Moth == 1) {
						if (end_Day > begin_Day)
							return getData(0, end_Moth - begin_Moth, end_Day
									- begin_Day);
						else
							return getData(0, 0,
									getMothDay(begin_Year, begin_Moth)
											- begin_Day + end_Day);
					} else {
						return getData(0, 0, end_Day - begin_Day);
					}
				} else {
					// ���1����������
					if (end_Year - begin_Year > 1
							|| (end_Year - begin_Year == 1 && end_Moth > begin_Moth)
							|| (end_Year - begin_Year == 1
									&& end_Moth == begin_Moth && end_Day >= begin_Day)) {
						if (end_Moth - begin_Moth > 1)// ���1����������
						{
							int day_count = getMothDay(begin_Year, begin_Moth)
									- begin_Day;
							day_count += end_Day;
							return getData(end_Year - begin_Year, end_Moth
									- begin_Moth, day_count);
						} else if (end_Moth - begin_Moth == 1) {
							if (end_Day >= begin_Day)
								return getData(end_Year - begin_Year, end_Moth
										- begin_Moth, end_Day - begin_Day);
							else
								return getData(end_Year - begin_Year, 0,
										getMothDay(begin_Year, begin_Moth)
												- begin_Day + end_Day);
						} else {
							if (end_Day >= begin_Day)
								return getData(end_Year - begin_Year , 0, end_Day
										- begin_Day+1);
							else
								return getData(end_Year - begin_Year - 1, 12
										- begin_Moth + end_Moth - 1,
										getMothDay(begin_Year, begin_Moth)
												- begin_Day + end_Day);
						}
					} else {
						if (end_Day >= begin_Day)
							return getData(0, 12 - begin_Moth + end_Moth,
									end_Day - begin_Day);
						else
							return getData(0, 12 - begin_Moth + end_Moth - 1,
									getMothDay(begin_Year, begin_Moth)
											- begin_Day + end_Day);
					}
				}
			}
			return "";
		}

		private static String getData(int year, int moth, int day) {
			return year + "-" + moth + "-" + day + "-";
		}

		private static int getMothDay(int year, int moth) { // ĳһ�µ�����
			if (moth == 2)
				if ((year % 4 == 0 && year % 100 == 0) || year % 400 == 0)
					return 29;
				else
					return 28;
			else
				switch (moth) {
				case 1:
				case 3:
				case 5:
				case 7:
				case 8:
				case 10:
				case 12:
					return 31;
				default:
					return 30;
				}
		}
}
